from urllib.parse import urlencode, parse_qs
# from urllib.request import urlopen
import requests,json
from django.test import TestCase

# Create your tests here.

# if __name__ == '__main__':
    # urlencode(dict)  :将字典转查询字符串
    # data={
    #     'a':11,
    #     'b':22,
    #     'c':33
    # }
    # res=urlencode(data)
    # print(res)   #c=33&a=11&b=22

# if __name__ == '__main__':
#     # parse_qs(qs):  将查询字符串转换为字典
#     qs='c=33&a=11&b=22'
#     res=parse_qs(qs)   #转换为字典,key对应的value类型是list
#     print(res)  #{'a': ['11'], 'c': ['33'], 'b': ['22']}


# 方法1:requests.get()
# if __name__ == '__main__':
#     # requests.get()  发起HTTP网络请求
#     req_url = 'http://api.meiduo.site:8000/mobiles/13155667788/count/'
#     #发起HTTP网络请求
#     response=requests.get(req_url)  #用urlopen链接被拒绝,换成request.get()
#     #获取响应数据
#     res_data=response.text   #res_data为字符串类型
#     print(repr(res_data))   #repr查看格式的类型,
#
#     # byte  -->str
#     res_data2=json.loads(res_data)   #解码json.loads
#     print(type(res_data2))   #为字典类型
#
#
# # 方法2:urlopen(url):  我的虚拟机不支持,老师可以用
# if __name__ == "__main__":
#     # urlopen(url): 发起http网络请求
#
#     req_url = 'http://api.meiduo.site:8000/mobiles/13155667788/count/'
#
#     # 发起http网络请求
#     response = urlopen(req_url)
#
#     # 获取响应数据
#     res_data = response.read() # 响应数据为bytes
#
#     # bytes->str
#     res_data = res_data.decode()
#     print(res_data)

from itsdangerous import TimedJSONWebSignatureSerializer as TJWSSer
from itsdangerous import BadData
# itsdangerous: 进行数据的加密和解密
# 安装: pip install itsdangerous

# if __name__ == '__main__':
#     #加密
#     data={
#         'openid':'KDKLSLK188KDLLSLDLLAKDL'
#     }
#     # TJWSSerializer(secret_key='密钥', expires_in='解密有效时间: s')
#     serializer=TJWSSer(secret_key='aaa111',expires_in=3600)
#     # res=ser.dumps(data)   #bytes类型    dumps加密

#     res=serializer.dumps(data).decode()   #str类型
#     print(res)    #eyJhbGciOiJIUzI1NiIsImlhdCI6MTU0NjYxMTI1NSwiZXhwIjoxNTQ2NjE0ODU1fQ.eyJvcGVuaWQiOiJLREtMU0xLMTg4S0RMTFNMRExMQUtETCJ9.hiMsRjO3228nJik-hhdh3I5DVsSJYcVLPflhqLdc9PY

    # 加解密的secret_key要一致才能解密成功

if __name__ == '__main__':
    data='eyJhbGciOiJIUzI1NiIsImlhdCI6MTU0NjYxMTI1NSwiZXhwIjoxNTQ2NjE0ODU1fQ.eyJvcGVuaWQiOiJLREtMU0xLMTg4S0RMTFNMRExMQUtETCJ9.hiMsRjO3228nJik-hhdh3I5DVsSJYcVLPflhqLdc9PY'
    serializer=TJWSSer(secret_key='aaa111')
    try:
        res = serializer.loads(data)   #loads解密
    except BadData:   #BadData所有解密错误的父类
        print('解密失败')
    else:
        print(res)   #{'openid': 'KDKLSLK188KDLLSLDLLAKDL'}  字典类型

